﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcWebShose.Models;
using MvcWebShose.Controllers;

namespace MvcWebShose.BUS
{
    public class SanPhamBUS
    {
        static WebShoseDataContext db = new WebShoseDataContext();
        public static List<SanPhamModels> ListSanPham()
        {
            List<SanPhamModels> lstModel = new List<SanPhamModels>();

            foreach (var item in db.SANPHAMs)
            {
                SanPhamModels sp = new SanPhamModels();
                sp.MA_SP = (int)item.MA_SP;
                sp.MA_NCC = (int)item.MA_NCC;
                sp.MA_LOAI =(int) item.MA_LOAI;
                sp.MOTA = item.MOTA;
                sp.GIA_SP = (float)item.GIA_SP;
                sp.HINHANH=item.HINHANH;
                sp.QUYCACH = item.QUYCACH;
                lstModel.Add(sp);
            }
            return lstModel;
        }
        public static SanPhamModels getDetailBySanPhamID(int MA_SP)
        {
            try
            {
                var result = (from p in db.SANPHAMs
                              where p.MA_SP == MA_SP
                              select p).First();
                return new SanPhamModels()
                {
                    MA_SP = result.MA_SP,
                    MA_LOAI =(int) result.MA_LOAI,
                    MA_NCC =(int) result.MA_NCC,
                    TEN_SP=result.TEN_SP,
                    GIA_SP = (float)result.GIA_SP,
                    MOTA = result.MOTA,
                    HINHANH=result.HINHANH,
                    QUYCACH = result.QUYCACH,
                    TRANGTHAI=result.TRANGTHAI
                };
            }
            catch
            {
                return null;
            }
        }

        // San pham moi
        public static List<SanPhamModels> getNewSanPham()
        {
            List<SanPhamModels> lstModel = new List<SanPhamModels>();

            foreach (var item in db.SANPHAMs)
            {
                SanPhamModels sp = new SanPhamModels();
                sp.MA_SP = item.MA_SP;
                sp.MA_NCC = item.MA_NCC;
                sp.MA_LOAI = item.MA_LOAI;
                sp.MOTA = item.MOTA;
                sp.GIA_SP = (float)item.GIA_SP;
                //sp.HINHANH=item.HINHANH;
                sp.QUYCACH = item.QUYCACH;
                lstModel.Add(sp);
            }
            return lstModel;
            //var rs = db.
        }

        // details
        public static SanPhamModels getSanPhamDetails(int ma_sp)
        {
            try
            {
                var result = (from p in db.SANPHAMs
                              where p.MA_SP == ma_sp
                              select p).First();
                // Có tồn tại result;
                return new SanPhamModels()
                {
                    MA_SP = result.MA_SP,
                    MA_LOAI = result.MA_LOAI,
                    MA_NCC=result.MA_NCC,
                    TEN_SP=result.TEN_SP,
                    GIA_SP=(float)result.GIA_SP,
                    MOTA=result.MOTA,
                    HINHANH=result.HINHANH,
                    QUYCACH=result.QUYCACH,
                    TRANGTHAI=result.TRANGTHAI
                };
            }
            catch
            {
                return null;
            }
        }

        public static void EditSanPham(SanPhamModels newitem)
        {
            try
            {
                var editsp = db.SANPHAMs.Select(p => p).Where(p => p.MA_SP == newitem.MA_SP).FirstOrDefault();
                editsp.MA_SP = newitem.MA_SP;
                editsp.MA_LOAI = newitem.MA_LOAI;
                editsp.MA_NCC = newitem.MA_NCC;
                editsp.TEN_SP = newitem.TEN_SP;
                editsp.GIA_SP = (decimal)newitem.GIA_SP;
                editsp.MOTA = newitem.MOTA;
                editsp.HINHANH = newitem.HINHANH;
                editsp.QUYCACH = newitem.QUYCACH;
                editsp.TRANGTHAI = newitem.TRANGTHAI;
                db.SubmitChanges();
            }
            catch
            {
            }
        }

        public static void DeleteSanPham(int masp)
        {
            try
            {
                var editsp = db.SANPHAMs.Select(p => p).Where(p => p.MA_SP == masp).FirstOrDefault();
                db.SANPHAMs.DeleteOnSubmit(editsp);

                db.SubmitChanges();
            }
            catch
            {
            }
        }
    }
}